
#include <BStyle>
#include <BPainter>
#include <BRender>
#include <BViewer>
#include <BScene>
#include <BCone>
#include <BCube>
#include <BImage>
#include <BMaterial>
#include <BReadWrite>

#include <BFixedRestraint>
#include "CurveSliderHandler.h"

CurveSliderHandler::CurveSliderHandler()
{
	_group = new BGroup();

	_nodeA = new BBody("nodeA");
	{
		_nodeA->setMass(0);
		_nodeA->setGravity(0, 0, -10);
		_nodeA->move(0, 0, 0.5);

		BCube* cube = new BCube(1, 12);
		cube->setColor(100, 200, 255);
		_nodeA->insertShape(cube);

		BVectorArray* vertices = new BVectorArray();
		vertices->append().set(0, 0, 1);
		vertices->append().set(1, 0, 1);
		vertices->append().set(1, 1, 1);
		vertices->append().set(1, 2, 1);
		vertices->append().set(0, 2, 1);
		vertices->append().set(0, 1, 1);
		vertices->append().set(-1, 1, 1);
		vertices->append().set(-2, -1, 1);

		_splineCurve = new BSplineCurve();
		_splineCurve->setVertices(vertices);
		_splineCurve->setCloseLoop(true);
		_splineCurve->setPointSize(10);
		_splineCurve->setPointSmooth(true);
		_splineCurve->setColor(123, 234, 0);
		_nodeA->insertShape(_splineCurve);

		_group->insert(_nodeA);
	}

	_nodeB = new BBody("nodeB");
	{
		_nodeB->setMass(1);
		_nodeB->setGravity(0, 0, -10);

		BCone* cone = new BCone(0.1, 0.2, 24, 10);
		cone->setColor(200, 100, 0);
		cone->setPolySmooth(true);
		_nodeB->insertShape(cone);
		_group->insert(_nodeB);
	}

	_nodeC = new BBody("nodeC");
	{
		_nodeC->setMass(1);
		_nodeC->setGravity(0, 0, -10);

		BCube* cube = new BCube(0.1, 0.1, 1, 12);
		cube->setColor(100, 200, 255);
		_nodeC->insertShape(cube);
		_group->insert(_nodeC);
	}

	_sliderRestraint = new BSliderRestraint(_nodeA, _nodeB);
	_sliderRestraint->setCurve(_splineCurve);
	_sliderRestraint->setAxisA(BVector(1, 0, 0));
	_sliderRestraint->setAxisB(BVector(1, 0, 0));
	_sliderRestraint->setPivotB(0, 0, -0.05);
	_sliderRestraint->setColliding(true);
	_group->insert(_sliderRestraint);
}
CurveSliderHandler::~CurveSliderHandler()
{

}

void CurveSliderHandler::attached(BViewer* viewer)
{
	BScene* dscene = viewer->scene();
	dscene->insert(_group);
	this->emit("edit-restraint", _sliderRestraint);
}
void CurveSliderHandler::detached(BViewer* viewer)
{
	BScene* dscene = viewer->scene();
	dscene->remove(_group);
}

void CurveSliderHandler::paint(BPainter& painter)
{
	BViewer* viewer = this->viewer();

}
void CurveSliderHandler::render(BRender& render)
{
	render.setLighting(false);
	render.setDepthTest(false);
	render.setLineSmooth(true);
	render.setLineWidth(1);

	if (_fixedRestraint)
	{
		const BVector& center = _fixedRestraint->property("center");
		render.setColor(255, 0, 0);
		render.setPointSize(10);
		render.drawVertex(center);

		const BBox& box = _fixedRestraint->property("box");
		render.setColor(0, 255, 255);
		render.drawBox(box);
	}
	if (_sliderRestraint)
	{
		render.setPointSize(6);

		const BMatrix& matrixA = _sliderRestraint->bodyA()->matrix();
		BVector pivotA = matrixA * _sliderRestraint->pivotA();
		BVector axisA = matrixA.mult3(_sliderRestraint->axisA());
		render.setColor(200, 0, 200);
		render.drawVertex(pivotA);
		render.drawLine(pivotA, pivotA + axisA);

		render.setLineStipple(0xf0f0);

		const BMatrix& matrixB = _sliderRestraint->bodyB()->matrix();
		BVector pivotB = matrixB * _sliderRestraint->pivotB();
		BVector axisB = matrixB.mult3(_sliderRestraint->axisB());
		render.setColor(0, 200, 200);
		render.drawVertex(pivotB);
		render.drawLine(pivotB, pivotB + axisB);

	}

}

void CurveSliderHandler::styleEvent(const BEvent& event)
{
	BHandler::styleEvent(event);
	const BStyle* style = event.value();
	BImage* image = style->image("cone-texture");
	if (BShape* shape = _nodeB->shape(0))
	{
		shape->setTexture(image);
	}
}
